/**
 * ElasticsearchIndexOperatorImpl.java
 *
 * @author: ZhuJiahui
 * @date: 2018/12/9 15:59
 */
package com.zhujiahui.elasticsearch.crud;

import com.alibaba.fastjson.JSONObject;
import com.zhujiahui.elasticsearch.connection.ElasticsearchConnection;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.xcontent.XContentType;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @description:
 * @author: ZhuJiahui
 * @version: 1.0
 */
public class ElasticsearchIndexOperatorImpl
        implements ElasticsearchIndexOperator {

    protected TransportClient transportClient =
            ElasticsearchConnection.getConnection();

    public ElasticsearchIndexOperatorImpl() {

    }

    @Override
    public boolean createIndex() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("orderNo",
                new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) +
                        "001");
        jsonObject.put("orderName", "购买元宝");
        jsonObject.put("orderTime", new Date());
        jsonObject.put("price", 1.5);
        jsonObject.put("ip", "127.0.0.1");

        IndexResponse
                response =
                this.transportClient.prepareIndex("rxpay", "order")
                        .setSource(jsonObject.toString(), XContentType.JSON)
                        .get();
        System.out.println("索引名称：" + response.getIndex());
        System.out.println("类型：" + response.getType());
        System.out.println("文档ID：" + response.getId()); // 第一次使用是1
        System.out.println("当前实例状态：" + response.status());
        return true;
    }

    @Override
    public void query() {
        try {
            GetResponse response =
                    this.transportClient.prepareGet("rxpay", "order",
                            "-0kCkmcB4WykHkOJQEAZ").execute().actionGet();
            System.out.println(response.getSourceAsString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateIndex() {
        JSONObject json = new JSONObject();
        json.put("user", "Joe");
        json.put("age", "22");
        json.put("sex", "男");
        json.put("orderTime", "6666666");
        UpdateResponse response = this.transportClient
                .prepareUpdate("rxpay", "order", "-0kCkmcB4WykHkOJQEAZ")
                .setDoc(json.toJSONString(), XContentType.JSON).get();
        System.out.println("索引名称：" + response.getIndex());
        System.out.println("类型：" + response.getType());
        System.out.println("文档ID：" + response.getId());
        System.out.println("当前实例状态：" + response.status());

    }

    @Override
    public boolean deleteIndex() {
        DeleteResponse response = this.transportClient
                .prepareDelete("rxpay", "order", "-0kCkmcB4WykHkOJQEAZ").get();
        System.out.println("索引名称：" + response.getIndex());
        System.out.println("类型：" + response.getType());
        System.out.println("文档ID：" + response.getId());
        System.out.println("当前实例状态：" + response.status());
        return false;
    }

    public static void main(String[] args) {
        ElasticsearchIndexOperatorImpl elasticsearchIndexOperator =
                new ElasticsearchIndexOperatorImpl();
        //elasticsearchIndexOperator.createIndex();

        elasticsearchIndexOperator.query();

        //elasticsearchIndexOperator.updateIndex();
    }
}
